package edu.stthomas.seis752.controllers;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.stthomas.seis752.model.Media;
import edu.stthomas.seis752.model.User;
import edu.stthomas.seis752.service.MediaService;
import edu.stthomas.seis752.service.UserService;

@Controller
public class ProfileController {

    @Autowired
    private MediaService mediaService;
    
    @Autowired
    private UserService userService;

    private static final Logger logger = LoggerFactory
            .getLogger(ProfileController.class);

    // take user to profile page
/*    @RequestMapping(value = "/myprofile", method = RequestMethod.GET)
    public String myprofile(){
        return "profile";
     }*/
	
	@RequestMapping(value = "/myprofile", method = RequestMethod.GET)
	public ModelAndView userprofile() {

		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
	    String name = auth.getName();
	    User currUser = userService.getUserByUserName(name);
	    
		ModelAndView mav = new ModelAndView("profile");
        mav.addObject("myprofile", currUser);
        
        
        
        return mav;
	}
	
     
    // this will be an ajax request so we will just reply with list of media see listMedia.jsp
    @RequestMapping(value = "/fetchMedia", method = RequestMethod.GET)
    public String fetchMedia(Locale locale, Model model, HttpServletResponse resp) {
        // obtain authenticated user's media list
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        User currUser = userService.getUserByUserName(name);
        Long userId = currUser.getId();
        List<Media> mediaList = mediaService.getUserMedia(userId);
        model.addAttribute("mediaList", mediaList);
        return "listMedia";
    }

}
